<?php
!function_exists('readover') && exit('Forbidden');
require_once(R_P.'require/watermark.php');
function ImgWaterMark($source,$w_pos=0,$w_img='',$w_text='',$w_font=5,$w_color='#FF0000',$w_pct,$w_quality){
ImgWaterMarkT1($source,$w_pos=0,$w_img,$w_text,$w_font,$w_color,$w_pct,$w_quality);
}

// Thumb with defined W and H
function MakeSThumb($srcFile,&$dstFile,$dstW,$dstH,$forceSize = null){
	if (!$dstW && $dstH) {
		$dstW = $dstH;
	} elseif (!$dstH && $dstW) {
		$dstH = $dstW;
	} elseif (!$dstH && !$dstW) {
		return false;
	}
	$minitemp = GetImgInfo($srcFile);
	if (empty($minitemp)) 
		return false;
	CompleteThumbInfo($minitemp,$dstW,$dstH,null,$forceSize);
	
	list($imagecreate,$imagecopyre) = GetImagecreate($minitemp['type']);
	if ((empty($sameFile) && $dstFile === $srcFile) || !$imagecreate) 
	    return false;
	
	$srcX = $minitemp['srcX'];
	$srcY = $minitemp['srcY'];
	$dstX = $dstY = 0;
	$thumb = $imagecreate($minitemp['dstW'],$minitemp['dstH']);
	$imagecopyre($thumb,$minitemp['source'],$dstX,$dstY,$srcX,$srcY,$minitemp['dstW'],$minitemp['dstH'],$minitemp['width'],$minitemp['height']);
	MakeImage($minitemp['type'],$thumb,$dstFile);
	imagedestroy($thumb);
	return array($minitemp['dstW'],$minitemp['dstH']);
}

function MakeThumb($srcFile,&$dstFile,$dstW,$dstH,$cenTer = null,$sameFile = null){
	if (!$dstW && $dstH) {
		$dstW = $dstH;
	} elseif (!$dstH && $dstW) {
		$dstH = $dstW;
	} elseif (!$dstH && !$dstW) {
		return false;
	}
	$minitemp = GetImgInfo($srcFile,$dstW,$dstH,$cenTer);
	if (empty($minitemp) || ($minitemp['width']<=$dstW && $minitemp['height']<=$dstH)) 
		return false;
	CompleteThumbInfo($minitemp,$dstW,$dstH,$cenTer);
	
	list($imagecreate,$imagecopyre) = GetImagecreate($minitemp['type']);
	if ((empty($sameFile) && $dstFile === $srcFile) || !$imagecreate) 
	    return false;
	!empty($sameFile) && $dstFile = $srcFile;
	
	$srcX = $minitemp['srcX'];
	$srcY = $minitemp['srcY'];
	$dstX = $dstY = 0;
	$thumb = $imagecreate($minitemp['dstW'],$minitemp['dstH']);
	$imagecopyre($thumb,$minitemp['source'],$dstX,$dstY,$srcX,$srcY,$minitemp['dstW'],$minitemp['dstH'],$minitemp['width'],$minitemp['height']);
	MakeImage($minitemp['type'],$thumb,$dstFile);
	imagedestroy($thumb);
	return array($minitemp['dstW'],$minitemp['dstH']);
}
function CompleteThumbInfo(&$imgdata,$dstW,$dstH,$cenTer = null,$forceSize = null){
	if (empty($imgdata)) return false;
	if (!empty($cenTer)) {
		if ($dstW > $dstH) {
			$dstW = $dstH;
		} else {
			$dstH = $dstW;
		}
		$forceSize = true;
	}
	
	if (empty($forceSize)) {
		$imgdata['srcX'] = $imgdata['srcY'] = 0;
		// dst W, caculate H
		$calcH = round($dstW/$imgdata['width']*$imgdata['height']);
		// caculate W, dst H
		$calcW = round($dstH/$imgdata['height']*$imgdata['width']);
		if ($calcW < $dstW) {		
			$imgdata['dstW'] = $calcW;
			$imgdata['dstH'] = $dstH;	
		} else {
			$imgdata['dstW'] = $dstW;
			$imgdata['dstH'] = $calcH;		
		}
	} else {
		$imgdata['dstW'] = $dstW;
		$imgdata['dstH'] = $dstH;
		// src W, caculate H
		$calcH = round($imgdata['width']/$dstW*$dstH);
		// caculate W, src H
		$calcW = round($imgdata['height']/$dstH*$dstW);	
		if ($calcW < $imgdata['width']) {		
			$imgdata['srcX'] = round(($imgdata['width'] - $calcW)/2);
			$imgdata['srcY'] = 0;
			$imgdata['width'] = $calcW;	
		} else {
			$imgdata['srcX'] = 0;
			$imgdata['dstH'] = round(($imgdata['height'] - $calcH)/2);
			$imgdata['height'] = $calcH;		
		}
	}
	return $imgdata;
}
function GetImgInfo($srcFile){
	$imgdata = (array)GetImgSize($srcFile);
	if ($imgdata['type']==1) {
		$imgdata['type'] = 'gif';
	} elseif ($imgdata['type']==2) {
		$imgdata['type'] = 'jpeg';
	} elseif ($imgdata['type']==3) {
		$imgdata['type'] = 'png';
	} elseif ($imgdata['type']==6) {
		$imgdata['type'] = 'bmp';
	} else {
		return false;
	}
	if (empty($imgdata) || !function_exists('imagecreatefrom'.$imgdata['type'])) {
		return false;
	}
	$imagecreatefromtype = 'imagecreatefrom'.$imgdata['type'];
	$imgdata['source']	 = $imagecreatefromtype($srcFile);
	!$imgdata['width'] && $imgdata['width'] = imagesx($imgdata['source']);
	!$imgdata['height'] && $imgdata['height'] = imagesy($imgdata['source']);
	return $imgdata;
}
function MakeImage($type,$image,$filename,$quality='75'){
	$makeimage = 'image'.$type;
	if (!function_exists($makeimage)) {
		return false;
	}
	if ($type == 'jpeg') {
		$makeimage($image,$filename,$quality);
	} else {
		$makeimage($image,$filename);
	}
}
function GetImgSize($srcFile,$srcExt = null){
	empty($srcExt) && $srcExt = strtolower(substr(strrchr($srcFile,'.'),1));
	$srcdata = array();
	if (function_exists('read_exif_data') && in_array($srcExt,array('jpg','jpeg','jpe','jfif'))) {
		$datatemp = @read_exif_data($srcFile);
		$srcdata['width'] = $datatemp['COMPUTED']['Width'];
		$srcdata['height'] = $datatemp['COMPUTED']['Height'];
		$srcdata['type'] = 2;
		unset($datatemp);
	}
	!$srcdata['width'] && list($srcdata['width'],$srcdata['height'],$srcdata['type']) = @getimagesize($srcFile);
	if (empty($srcdata) || !$srcdata['type']) {
		return false;
	}
	return $srcdata;
}
function GetImagecreate($imagetype){
	if ($imagetype != 'gif' && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled')) {
		return array('imagecreatetruecolor','imagecopyresampled');
	} elseif (function_exists('imagecreate') && function_exists('imagecopyresized')) {
		return array('imagecreate','imagecopyresized');
	} else {
		return array();
	}
}
?>